/**
 * Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.router;

import com.rshang.rsdp.service.ITbDictoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * 功能简介：<br/>
 *
 * @author zhangjun
 * @create 2017年8月14日上午10:22:25
 */
@Controller
@RequestMapping("/system/dic")
public class DicRouter extends BaseRouter {

    @Autowired
    ITbDictoryService dicService;

    /**
     * 数据字典首页
     */
    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("system/dic_index");
    }

    /**
     * 新增数据字典
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        return new ModelAndView("system/dic_add");
    }

    /**
     * 数据字典编辑
     */
    @RequestMapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView("system/dic_edit");
        view.addObject("result", dicService.get(request));
        return view;
    }

    /**
     * 模板导入
     */
    @RequestMapping("/import")
    public ModelAndView importData() {
        return new ModelAndView("system/dic_import");
    }

    @RequestMapping("/download")
    public ResponseEntity<FileSystemResource> download() throws IOException {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", "vnd.ms-excel"));
        header.set("Content-disposition", "attachment;filename=dic-v1.0.xls");
        Resource resource = new ClassPathResource("xls/dic-v1.0.xls");
        return new ResponseEntity<>(new FileSystemResource(resource.getFile()), header, HttpStatus.OK);
    }
}
